package com.systop.asset.controller;

import java.util.List;

import com.systop.asset.entity.Location;
import com.systop.asset.entity.RestResponse;
import com.systop.asset.entity.vo.AssetCategoryVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.systop.asset.entity.AssetCategories;
import com.systop.asset.service.IAssetCategoriesService;


/**
 * 资产分类Controller
 * 
 * @author mc
 * @date 2025-06-24
 */
@RestController
@RequestMapping("/system/assetCategories")
public class AssetCategoriesController
{
    @Autowired
    private IAssetCategoriesService assetCategoriesService;

    /**
     * 查询资产分类列表
     */
    @GetMapping("/list")
    public RestResponse list(AssetCategories assetCategories)
    {

        List<AssetCategories> list = assetCategoriesService.getCategories(assetCategories);
        return RestResponse.success(list);
    }



    /**
     * 获取资产分类详细信息
     */
    @GetMapping(value = "/{id}")
    public RestResponse getInfo(@PathVariable("id") Long id)
    {
        return RestResponse.success(assetCategoriesService.selectAssetCategoriesById(id));
    }

    /**
     * 新增资产分类
     */
    @PostMapping
    public RestResponse add(@RequestBody AssetCategories assetCategories)
    {
        if(assetCategoriesService.insertAssetCategories(assetCategories)!=0){
            return RestResponse.success();
        }
        return RestResponse.failure(500,"添加失败,名称已存在");
    }

    /**
     * 修改资产分类
     */
    @PutMapping
    public RestResponse edit(@RequestBody AssetCategories assetCategories)
    {
        if(assetCategoriesService.updateAssetCategories(assetCategories)!=0){
            return RestResponse.success();
        }
        return RestResponse.failure(500,"修改失败,名称已存在");
    }

    /**
     * 删除资产分类
     */
	@DeleteMapping("/{ids}")
    public RestResponse remove(@PathVariable Long[] ids)
    {
        //删除时判断是否包含子分类
        if(assetCategoriesService.selectAssetCategoriesById(ids[0]).getParentId() == 0L){
            return RestResponse.failure(500,"包含子分类，请先删除子分类");
        }
        return RestResponse.success(assetCategoriesService.deleteAssetCategoriesByIds(ids));
    }
    /**
     * 查询资产分类列表
     */
    @GetMapping("/allList")
    public RestResponse allList(AssetCategories assetCategories)
    {
        List<AssetCategoryVo> list = assetCategoriesService.allList(assetCategories);
        return RestResponse.success(list);
    }
}
